package com.derek.omshopping.ui.classify;

import android.util.Log;

import com.derek.omshopping.bean.classify.ClassifyRightTypeBean;
import com.derek.omshopping.config.UrlConfig;
import com.derek.omshopping.http.RetrofitService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by My on 2016/10/19.
 */

public class ClassifyRightTypeModel implements ClassifyRightTypeContract.Model {
    private List<ClassifyRightTypeBean> list_right=new ArrayList<>();
    private String tag_img_path;

    @Override
    public void getRightTypeView(final String code_mgroup, final onGetRightTypeListener getRightTypeListener) {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(UrlConfig.CLASSIFY_RIGHT_BASEURL)
                .build();
        Call<ResponseBody> call=retrofit.create(RetrofitService.class).getClassifyRightRecyclerView(code_mgroup);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json=response.body().string();
                    Log.i("tagtag",json+"<><><><><><><><><><><>");
                    JSONObject obj=new JSONObject(json);
                    JSONArray arr=obj.getJSONArray("middleCategories");
                    for(int i=0;i<arr.length();i++){
                        if(i==0){
                            JSONObject obj3=arr.getJSONObject(0);
                            tag_img_path=obj3.getString("tag_img_path");
                        }else{
                            JSONObject obj1=arr.getJSONObject(i);
                            JSONArray arr1=obj1.getJSONArray("categoryList");
                            for(int j=0;j<arr1.length();j++){
                                JSONObject obj2=arr1.getJSONObject(j);
                                String shop_nm_3=obj2.getString("shop_nm_3");
                                ClassifyRightTypeBean classifyRightTypeBean =new ClassifyRightTypeBean(shop_nm_3);
                                list_right.add(classifyRightTypeBean);
                                Log.i("tag","右侧集合长度为："+list_right.size());

                            }

                        }
                    }
                    getRightTypeListener.LoadRightTypeUrl(list_right,tag_img_path);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }

    public interface onGetRightTypeListener{
        void LoadRightTypeUrl(List<ClassifyRightTypeBean> list_right,String tag_img_path);
    }
}
